﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TeamBuy.Common;

namespace TeamBuy.AccessService
{
    public class TeamBuyAdminService : Common.ITeamBuyAdmin
    {

        public int CreateAdmin(string adminName, string adminPwd, int flag)
        {
            int hasAdmin;
            hasAdmin = TeamBuy.AccessService.AccessHelper.GetScalar("select * from S_Admin where AdminName='" + adminName + "'");
            if (hasAdmin > 0)
            {
                return 2;
            }
            else
            {
                int regAdmin;
                regAdmin = TeamBuy.AccessService.AccessHelper.ExecuteCommand("insert into C_Admin(AdminName, AdminPwd, Flag) values('" + adminName + "','" + PublicFunction.MD5(adminPwd) + "'," + flag + ")");
                if (regAdmin == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

        public int AdminLogin(string adminName, string adminPwd)
        {
            DataTable dtAdmin;
            dtAdmin = TeamBuy.AccessService.AccessHelper.GetDataSet("select * from S_Admin where AdminName='" + adminName + "' and AdminPwd='" + PublicFunction.MD5(adminPwd) + "'");
            if (dtAdmin.Rows.Count == 1)
            {
                if (Convert.ToInt32(dtAdmin.Rows[0]["AdminID"]) > 0 && Convert.ToBoolean(dtAdmin.Rows[0]["IsActived"]) == true)
                {
                    return Convert.ToInt32(dtAdmin.Rows[0]["AdminID"]);
                }
                else if (Convert.ToInt32(dtAdmin.Rows[0]["AdminID"]) > 0 && Convert.ToBoolean(dtAdmin.Rows[0]["IsActived"]) == false)
                {
                    return -1;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return 0;
            }
        }

        public System.Data.DataTable GetAdmin(int adminID)
        {
            return TeamBuy.AccessService.AccessHelper.GetDataSet("select * from S_Admin where AdminID=" + adminID);
        }

        public bool IsAdminNameUsed(string adminName)
        {
            throw new NotImplementedException();
        }

        public int ChangeAdminPassword(int adminID, string oldPwd, string newPwd)
        {
            throw new NotImplementedException();
        }

        public bool UpdateAdminFlag(int adminID, int flag)
        {
            throw new NotImplementedException();
        }
    }
}
